package cn.kgc.day05.service.impl;

import cn.hutool.crypto.SecureUtil;
import cn.kgc.day05.dao.UserDao;
import cn.kgc.day05.dao.impl.UserDaoImpl;
import cn.kgc.day05.entity.User;
import cn.kgc.day05.http.ResponseResult;
import cn.kgc.day05.service.UserService;

/**
 * @author 课工场
 * @date 2024/10/16
 * @description
 */
public class UserServiceImpl implements UserService {

    private UserDao userDao = new UserDaoImpl();

    @Override
    public ResponseResult selectByUsername(String username) {
        ResponseResult responseResult = new ResponseResult();
        User user =  userDao.selectByUsername(username);
        if (user==null){
            responseResult.setCode(0);
            responseResult.setMsg("用户名可用");
        }else {
            responseResult.setCode(110);
            responseResult.setMsg("用户名已存在");
        }

        return responseResult;
    }

    @Override
    public ResponseResult registerUser(String username, String password) {

        ResponseResult responseResult = new ResponseResult();
        // 判定用户名是否存在
        User user = userDao.selectByUsername(username);
        if (user!=null){
            responseResult.setCode(110);
            responseResult.setMsg("用户名已存在");
            return  responseResult;
        }
        // 加密密码
        String md5Pwd = SecureUtil.md5(password);

        try {
            userDao.registerUser(username,md5Pwd);
            responseResult.setCode(0);
            responseResult.setMsg("注册成功");
        }catch (Exception e){
            responseResult.setCode(110);
            responseResult.setMsg("注册失败");
        }

        return responseResult;
    }

    @Override
    public ResponseResult login(String username, String password) {

        String md5pwd = SecureUtil.md5(password);

        User  user =   userDao.selectByUsernameAndPwd(username,md5pwd);
        ResponseResult responseResult = new ResponseResult();
        if (user==null){
            responseResult.setCode(110);
            responseResult.setMsg("用户名或密码错误");
        }else {
            responseResult.setCode(0);
            responseResult.setMsg("认证通过");
        }

        return responseResult;
    }

    @Override
    public ResponseResult personalInfo(String username) {

        User user = userDao.selectByUsername(username);

        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(0);
        responseResult.setData(user);

        return responseResult;
    }

    @Override
    public ResponseResult editPersonalInfo(User user) {

        ResponseResult responseResult = new ResponseResult();
        try {
            userDao.updatePersonalInfo(user);
            responseResult.setCode(0);
            responseResult.setMsg("操作成功");
        }catch (Exception e){
            responseResult.setCode(110);
            responseResult.setMsg("操作失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult editPersonalPwd(String oldPwd, String newPwd, String username) {

        ResponseResult responseResult = new ResponseResult();

        //  判定用户的原始密码是否正确
        User user = userDao.selectByUsername(username);
        String DBPwd = user.getPassword();
        String md5OldPwd = SecureUtil.md5(oldPwd);

        if (!DBPwd.equals(md5OldPwd)){
            responseResult.setCode(110);
            responseResult.setMsg("原始密码不正确");
            return  responseResult;
        }

        String md5NewPwd = SecureUtil.md5(newPwd);

        try {
            userDao.updatePersonalPwd(username,md5NewPwd);
            responseResult.setCode(0);
            responseResult.setMsg("操作成功");
        }catch (Exception e){
            responseResult.setCode(110);
            responseResult.setMsg("操作失败");
        }

        return responseResult;
    }
}
